package juc.design.asyn.production;

import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;
import java.util.List;

import static juc.Sleeper.sleep;


//异步模式——生产者消费者模式
@Slf4j(topic = "c.Main")
public class Main {
    public static void main(String[] args) {
        MessageQueue messageQueue = new MessageQueue(2);

        for (int i = 0; i < 4; i++) {
            sleep(1);
            int id = i;
            new Thread(()->{
                List<String> response = Arrays.asList("a"+ id);
                log.debug("尝试放入消息队列({})", id);
                messageQueue.put(new Message(id, response));
            },"生产者"+i).start();
        }

        // 1 个消费者线程, 处理结果
        new Thread(() -> {
            while (true) {
                Message message = messageQueue.take();
                sleep(1);
                List<String> response = (List<String>) message.getMessage();
                log.debug("取出货({}): [{}] lines,[{}]value", message.getId(), response.size(), response.get(response.size()-1));
            }
        }, "消费者").start();
    }
}
